package org.jeecg.modules.tankerRegistration.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.tankerRegistration.entity.TankerRegistration;
import org.jeecg.modules.tankerRegistration.service.ITankerRegistrationService;
import org.jeecg.modules.tankerRules.entity.TankerRules;
import org.jeecg.modules.tankerRules.service.ITankerRulesService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Configuration
@EnableScheduling
@Slf4j
public class MyScheduled {

    private final ITankerRegistrationService tankerRegistrationService;
    private final ITankerRulesService tankerRulesService;

    public MyScheduled(ITankerRegistrationService tankerRegistrationService, ITankerRulesService tankerRulesService) {
        this.tankerRegistrationService = tankerRegistrationService;
        this.tankerRulesService = tankerRulesService;
    }

    @Scheduled(cron = "0 0 9-17 * * ?")//朝九晚五的每小时执行一次
    public void configureTasks() throws Exception{
        Map<String, TankerRules> map = new HashMap<>();
        List<TankerRules> list = tankerRulesService.list();
        for (TankerRules tankerRules : list) {
            map.put(tankerRules.getLocation(), tankerRules);
        }
        LambdaQueryWrapper<TankerRegistration> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TankerRegistration::getEntryStatus, 1);
        wrapper.notIn(TankerRegistration::getAuditStatus,3, 8, 9, 10, 11, 12, 14);
        List<TankerRegistration> tankerRegistrations = tankerRegistrationService.list(wrapper);
        List<TankerRegistration> updatedTankerRegistrations = new ArrayList<>();
        for (TankerRegistration tankerRegistration : tankerRegistrations) {
            String location = tankerRegistration.getLocation();
            if (StringUtils.isNotBlank(location) && map.containsKey(location)) {
                TankerRules tankerRules = map.get(location);
                if (tankerRules.getRegister()){
                    Integer registerTime = tankerRules.getRegisterTime();
                    Date registerDate = tankerRegistration.getRegisterDate();
                    if (isExpire(registerDate, registerTime)) {//是否过期
                        tankerRegistration.setAuditStatus(13);
                        updatedTankerRegistrations.add(tankerRegistration);
                    }
                }
                if (tankerRules.getAudit() && tankerRegistration.getAuditDate() != null){
                    Integer auditTime = tankerRules.getAuditTime();
                    Date auditDate = tankerRegistration.getAuditDate();
                    if (isExpire(auditDate, auditTime)) {//是否过期
                        tankerRegistration.setAuditStatus(13);
                        updatedTankerRegistrations.add(tankerRegistration);
                    }
                }
            }
        }
        if (!updatedTankerRegistrations.isEmpty()){
            tankerRegistrationService.updateBatchById(updatedTankerRegistrations);
        }
    }

    //是否过期
    private static boolean isExpire(Date registerDate, Integer registerTime) {
        // 将Date转换为LocalDateTime
        LocalDateTime registerLocalDateTime = registerDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        // 当前时间转换为LocalDateTime
        LocalDateTime currentTime = LocalDateTime.now();
        // 将registerDate加上registerTime小时
        LocalDateTime expirationTime = registerLocalDateTime.plusHours(registerTime);
        return expirationTime.isBefore(currentTime);
    }

}
